package com.ruoyi.td.service.impl;

import com.ruoyi.common.utils.JsonUtils;
import com.ruoyi.td.dao.TdTemplate;
import com.ruoyi.td.entity.ThingModelMessage;
import com.ruoyi.td.service.IThingModelMessageData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
public class ThingModelMessageDataImpl implements IThingModelMessageData {

    //@Resource
    private TdTemplate tdTemplate;
    @Override
    public void add(ThingModelMessage msg) {
        //使用deviceId作表名
        String sql = String.format("INSERT INTO %s (%s) USING %s TAGS ('%s') VALUES (%s);",
                "thing_model_message_" + msg.getDeviceId().toLowerCase(),
                "time,product_key,device_name,group_id,uid,identifier,data,report_time",
                "thing_model_message",
                msg.getDeviceId(),
                "?,?,?,?,?,?,?,?"
        );

        tdTemplate.update(sql, msg.getTime(),
                msg.getProductKey(), msg.getDeviceName(),
                msg.getGroupId(), msg.getUid(),
                msg.getIdentifier(),
                msg.getData() == null ? "{}" : JsonUtils.toJsonString(msg.getData()),
                msg.getTime() );
    }
}
